// couponList.js
const regeneratorRuntime = require('../../lib/runtime');
const wx = require('../../lib/wx');
const Api = require('../../utils/api.js');
const wxapp = require('../../lib/wxapp');
const Util = require('../../utils/util.js');

// 获取应用实例
const app = getApp();
Page({
  data: {
    isEmpty: true,
    couponList: [
      {
        title: '新用户优惠券满200减20元',
        range: '全场通用',
        date: '2018.02.13',
        isOverdue: false,
      },
      {
        title: '新用户优惠券满100减10元',
        range: '全场通用',
        date: '2018.01.13',
        isOverdue: true,
      },
    ],
  },

  onShow: wxapp.handler(function* onShow(next) {
    const authTokenKey = Util.authTokenKey();
    const token = wx.getStorageSync(authTokenKey);
    const url = '../login/login';
    if (!token) {
      wx.reLaunch({
        url,
      });
      return ;
    }
    var that = this;
    var getCouponList = Api.getCouponList();
    wx.request({
      url: getCouponList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {
        token: token
      },
      complete: function (res) {
        that.setData({ couponList:res.data.data});
        if (res.data.data.length > 0 ){
          that.setData({ isEmpty: false });
        }
      }
    });
    yield next;
  }),

  onLoad: wxapp.handler(function* onLoad(next) {
    // const pageData = this.data;
    // const couponList = pageData.couponList;
    // if(couponList.length > 0){
    //   this.setData({
    //     isEmpty: false,
    //   });
    // }

    yield next;
  }),

});
